package m202401.d01;

import java.util.Calendar;
import java.util.HashMap;
import java.util.Map;

/**
 * @@author: 爱做梦的锤子
 * @date: 2024/1/1 09:12
 */
//290. 单词规律
public class Q290 {
    public boolean wordPattern(String pattern, String s) {
        Map<Character, String> p2s = new HashMap<>();
        Map<String, Character> s2p = new HashMap<>();
        String[] split = s.split(" ");
        if (pattern.length() != split.length) {
            return false;
        }
        for (int i = 0; i < pattern.length(); i++) {
            char p = pattern.charAt(i);
            String se = split[i];
            if (p2s.containsKey(p) && !p2s.get(p).equals(se)) {
                return false;
            }
            if (s2p.containsKey(se) && s2p.get(se) != p) {
                return false;
            }
            p2s.put(p, se);
            s2p.put(se, p);
        }
        return true;
    }
}
